# -*- coding: utf-8 -*-
from MySpi import *
import thosttraderapi as api
import configparser
import threading
import os
import mylog


configins=configparser.ConfigParser()
configins.read('config.ini')
#Addr
FrontAddr=configins['network']['tradefront']
#AuthInfo
APPID=configins['authinfo']['appid']
AUTHCODE=configins['authinfo']['authcode']
#LoginInfo
BROKERID=configins['baseinfo']['brokerid']
USERID=configins['baseinfo']['userid']
PASSWORD=configins['baseinfo']['password']
#search conf
TIMEOUTSEC=int(configins['searchopt']['timeoutsec'])

#create logfile
mylog = mylog.get_logger("daylog")


class CTradeApi(api.CThostFtdcTraderApi):
	def __init__(self):
		self.tspi=''
		self.tapi=''
		self.mddata=[]
		self.insdata=[]
		self.mylog=mylog
		self.ctlevent=threading.Event()

	def start(self):
		self.tapi=api.CThostFtdcTraderApi.CreateFtdcTraderApi()
		self.tspi=CTradeSpi(self)
		self.tapi.RegisterFront(FrontAddr)
		self.tapi.RegisterSpi(self.tspi)
		self.tapi.SubscribePrivateTopic(api.THOST_TERT_RESTART)
		self.tapi.SubscribePublicTopic(api.THOST_TERT_RESTART)
		self.tapi.Init()
		self.ctlevent.wait(timeout = TIMEOUTSEC)
		if(self.ctlevent.isSet() == True):
			self.ctlevent.clear()
			return 0
		else:
			mylog.error("init failed, please check network env")
			return 233
			os._exit(233)

	def ReqAuth(self):
		authfield = api.CThostFtdcReqAuthenticateField()
		authfield.BrokerID=BROKERID
		authfield.UserID=USERID
		authfield.AppID=APPID
		authfield.AuthCode=AUTHCODE
		self.tapi.ReqAuthenticate(authfield,0)
		PrintSendName("ReqAuthenticate", mylog)
		self.ctlevent.wait(timeout = TIMEOUTSEC)
		if(self.ctlevent.isSet() == True):
			self.ctlevent.clear()
			return 0
		else:
			return 233
			os._exit(233)

	def ReqLogin(self):
		loginfield = api.CThostFtdcReqUserLoginField()
		loginfield.BrokerID=BROKERID
		loginfield.UserID=USERID
		loginfield.Password=PASSWORD
		loginfield.UserProductInfo="python dll"
		self.tapi.ReqUserLogin(loginfield,0)
		PrintSendName("ReqUserLogin", mylog)
		self.ctlevent.wait(timeout = TIMEOUTSEC)
		if(self.ctlevent.isSet() == True):
			self.ctlevent.clear()
			return 0
		else:
			return 233
			os._exit(233)

	def ReqQryMD(self):
		self.mddata.clear()
		mdfield = api.CThostFtdcQryDepthMarketDataField()
		self.tapi.ReqQryDepthMarketData(mdfield, 0)
		PrintSendName("ReqQryDepthMarketData", mylog)
		self.ctlevent.wait(timeout = TIMEOUTSEC)
		if(self.ctlevent.isSet() == True):
			self.ctlevent.clear()
			return self.mddata
		else:
			return 233
			os._exit(233)
	
	def ReqQryClsIns(self):
		self.insdata.clear()
		insfield = api.CThostFtdcQryClassifiedInstrumentField()
		insfield.TradingType='0'
		insfield.ClassType='0'
		self.tapi.ReqQryClassifiedInstrument(insfield, 0)
		PrintSendName("ReqQryClassifiedInstrument", mylog)
		self.ctlevent.wait(timeout = TIMEOUTSEC)
		if(self.ctlevent.isSet() == True):
			self.ctlevent.clear()
			return self.insdata
		else:
			return 233
			os._exit(233)

	def ReqQryIns(self):
		self.insdata.clear()
		insfield = api.CThostFtdcQryInstrumentField()
		self.tapi.ReqQryInstrument(insfield, 0)
		PrintSendName("ReqQryInstrument", mylog)
		self.ctlevent.wait(timeout = TIMEOUTSEC)
		if(self.ctlevent.isSet() == True):
			self.ctlevent.clear()
			return 0
		else:
			return 233
			os._exit(233)


#print func name
def PrintSendName(sendname, mylog):
	sendmsg = "send " + sendname + " ok"
	mylog.info(sendmsg)
